package cn.est.service.impl;
import cn.est.config.OssConfig;
import cn.est.dto.ModelReq;
import cn.est.mapper.ModelMapper;
import cn.est.pojo.Model;
import cn.est.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import cn.est.dto.Page;
import cn.est.constants.Constants;

import javax.annotation.Resource;

@Service
public class ModelServiceImpl implements ModelService {

    @Resource
    private ModelMapper modelMapper;

    @Resource
    OssConfig ossConfig;
     public Model getModelById(Long id){
        Model model = modelMapper.getModelById(id);
         if(model.getFaceImg()!=null&& !model.getFaceImg().equals(""))
         model.setFaceImg(ossConfig.getOssWebUrl()+model.getFaceImg());
         if(model.getContentImg()!=null&& !model.getContentImg().equals(""))
         model.setContentImg(ossConfig.getOssWebUrl()+model.getContentImg());
         return model;
    }

    public List<Model>	getModelListByMap(Map<String,Object> param){
        return modelMapper.getModelListByMap(param);
    }

    public Integer getModelCountByMap(Map<String,Object> param){
        return modelMapper.getModelCountByMap(param);
    }

    public Integer qdtxAddModel(Model model){
            model.setCreatdTime(new Date());
            return modelMapper.insertModel(model);
    }

    public Integer qdtxModifyModel(Model model){
        model.setUpdatedTime(new Date());
        return modelMapper.updateModel(model);
    }

    public Integer qdtxDeleteModelById(Long id){
        return modelMapper.deleteModelById(id);
    }

    public Integer qdtxBatchDeleteModel(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return modelMapper.batchDeleteModel(param);
    }

    public Page<Model> queryModelPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = modelMapper.getModelCountByMap(param);
        pageNo = pageNo==null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize==null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Model> modelList = modelMapper.getModelListByMap(param);
        page.setList(modelList);
        return page;
     }

    @Override
    public Page<Model> getModelPage(ModelReq modelReq) {
        //1.按照搜索条件获取总记录数 ----》分页条
        Integer total=modelMapper.getModelCount(modelReq);
        Page<Model> page=new Page<Model>(modelReq.getPageNo(),modelReq.getPageSize(),total);
        //2、查询商品列表
        modelReq.setPageNo(page.getBeginPos());
        List<Model> modelList= modelMapper.selectList(modelReq);

        for (Model model:modelList
             ) {
            if(model.getFaceImg()!=null&& !model.getFaceImg().equals(""))
                model.setFaceImg(ossConfig.getOssWebUrl()+model.getFaceImg());
            if(model.getContentImg()!=null&& !model.getContentImg().equals(""))
                model.setContentImg(ossConfig.getOssWebUrl()+model.getContentImg());
        }
        //3.封装结果返回
        page.setList(modelList);
        return page;
    }


}
